package com.ruoyi.system.service.project.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.project.firstclass.FirstClass;
import com.ruoyi.system.domain.project.firstclass.FirstClassMember;
import com.ruoyi.system.mapper.project.FirstClassMapper;
import com.ruoyi.system.mapper.project.FirstClassMemberMapper;
import com.ruoyi.system.service.project.IFirstClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 一流学科Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-28
 */
@Service
public class FirstClassServiceImpl implements IFirstClassService
{
    @Autowired
    private FirstClassMapper firstClassMapper;
    @Autowired
    private FirstClassMemberMapper firstClassMemberMapper;
    /**
     * 查询一流学科
     * 
     * @param id 一流学科主键
     * @return 一流学科
     */
    @Override
    public FirstClass selectFirstClassById(Long id)
    {
        FirstClass firstClass = firstClassMapper.selectFirstClassById(id);
        FirstClassMember firstClassMember = new FirstClassMember();
        firstClassMember.setClassId(id);
        List<FirstClassMember> firstClassMembers = firstClassMemberMapper.selectFirstClassMemberList(firstClassMember);
        firstClass.setFirstClassMembers(firstClassMembers);
        return firstClass;
    }

    /**
     * 查询一流学科列表
     * 
     * @param firstClass 一流学科
     * @return 一流学科
     */
    @Override
    public List<FirstClass> selectFirstClassList(FirstClass firstClass)
    {
        //如果是老师，则只显示自己上传的
        if(firstClass.getCurrentRole()!=null&&firstClass.getCurrentRole().equals("teacher")){
            firstClass.setTeacherId(getUserId());
        }
        //如果是部门，则显示老师是系里的所有
        if(firstClass.getCurrentRole()!=null&&firstClass.getCurrentRole().equals("department")){
            firstClass.setDeptId(SecurityUtils.getDeptId());
        }
        return firstClassMapper.selectFirstClassList(firstClass);
    }

    /**
     * 新增一流学科
     * 
     * @param firstClass 一流学科
     * @return 结果
     */
    @Override
    public int insertFirstClass(FirstClass firstClass)
    {
        if(firstClass.getId()!=null){
            FirstClassMember firstClassMember = new FirstClassMember();
            firstClassMember.setClassId(firstClass.getId());
            firstClassMember.setMemberId(firstClass.getTeacherId());
            firstClassMember.setMemberName(firstClass.getTeacherName());
            firstClassMemberMapper.insertFirstClassMember(firstClassMember);
        }else {
            int result = firstClassMapper.insertFirstClass(firstClass);
            if (result > 0) {
                // 插入成功，获取刚插入的id
                Long firstClassId = firstClass.getId();
                // 插入关联表
                FirstClassMember firstClassMember = new FirstClassMember();
                firstClassMember.setClassId(firstClassId);
                firstClassMember.setMemberId(firstClass.getTeacherId());
                firstClassMember.setMemberName(firstClass.getTeacherName());
                firstClassMemberMapper.insertFirstClassMember(firstClassMember);
            }
        }
        return 1;
    }

    /**
     * 修改一流学科
     * 
     * @param firstClass 一流学科
     * @return 结果
     */
    @Override
    public int updateFirstClass(FirstClass firstClass)
    {
        return firstClassMapper.updateFirstClass(firstClass);
    }

    /**
     * 批量删除一流学科
     * 
     * @param ids 需要删除的一流学科主键
     * @return 结果
     */
    @Override
    public int deleteFirstClassByIds(Long[] ids)
    {
        for (Long id : ids){
            FirstClassMember firstClassMember = new FirstClassMember();
            firstClassMember.setClassId(id);
            List<FirstClassMember> firstClassMembers = firstClassMemberMapper.selectFirstClassMemberList(firstClassMember);
            for (FirstClassMember firstClassMember1 : firstClassMembers) {
                firstClassMemberMapper.deleteFirstClassMemberById(firstClassMember1.getId());
            }
        }
        return firstClassMapper.deleteFirstClassByIds(ids);
    }

    /**
     * 删除一流学科信息
     * 
     * @param id 一流学科主键
     * @return 结果
     */
    @Override
    public int deleteFirstClassById(Long id)
    {
        return firstClassMapper.deleteFirstClassById(id);
    }
    /**
     * 根据标题查询
     * @param data
     * @return
     */
    @Override
    public FirstClass getInfoByTitle(String data) {
        return firstClassMapper.getInfoByTitle(data);
    }
}
